# TCP连接
import socket
import threading

def start_server(sock):
    while True:
        soct, addr = sock.accept()
        soct.send(b'Welcome')
        # 建立连接
        do_link(soct, addr)
def do_link(soct, addr):
    while True:
        # 两端通信中
        recv_bytes = soct.recv(1024)
        if not recv_bytes or recv_bytes.decode('UTF-8') == 'exit':
            break
        soct.send(('hello {0}'.format(recv_bytes.decode('UTF-8'))).encode('UTF-8'))

    print('addr={0} closed'.format(addr))
    # 断开连接
    soct.close()
if __name__ == '__main__':
    # AF_INET表示 ipv4, SOCK_STREAM表示 tcp连接
    soct = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    soct.bind(('127.0.0.1', 9527))
    soct.listen(5)
    thread = threading.Thread(target=start_server, args=(soct,))
    thread.start()


